package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
public class testController {

    @RequestMapping("/test")
    public String test() {
        return "Hello";
    }

    @RequestMapping("/file/pdfzip")
    public void  migration(HttpServletResponse response) throws Exception {

        String codedFileName = "test";
        response.setHeader("Content-Disposition", "attachment;filename=" +  codedFileName + ".zip");
        // 响应类型,编码
        response.setContentType("application/octet-stream;charset=UTF-8");
        // 形成输出流
        OutputStream osOut = response.getOutputStream();
        File zipFileC = new
                File("/java/src/main/resources/Files/test.zip");
        InputStream input = null;
        try {
            input = new FileInputStream(zipFileC);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                osOut.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            osOut.close();
        }
    }
}
